﻿@using Smartstore.Shipping.Models

@model ByTotalModel

<datagrid id="shipping-rates-bytotal-grid" class="h-100"
          allow-resize="true"
          allow-row-selection="true"
          allow-column-reordering="true"
          allow-edit="true">
    <datasource read="@Url.Action("ShippingRateByTotalList", "ByTotal")"
                update="@Url.Action("ShippingRateByTotalUpdate", "ByTotal")"
                delete="@Url.Action("ShippingRateByTotalDelete", "ByTotal")" />
    <paging position="Bottom" page-index="1" show-size-chooser="true" />
    <sorting allow-unsort="true" allow-multisort="true">
        <sort by="StoreId" />
        <sort by="CountryId" />
        <sort by="StateProvinceId" />
        <sort by="Zip" />
        <sort by="ShippingMethodId" />
        <sort by="From" />
    </sorting>
    <toolbar>
        <toolbar-group>
            <a class="btn btn-primary btn-flat" href="#addrecord-window" data-toggle="modal">
                <i class="fa fa-plus"></i>
                <span>@T("Plugins.Shipping.ByTotal.AddNewRecordTitle")</span>
            </a>
            <button datagrid-action="DataGridToolAction.SaveChanges" type="button" class="btn btn-warning no-anims btn-flat">
                <i class="fa fa-check"></i>
                <span>@T("Admin.Common.SaveChanges")</span>
            </button>
            <button datagrid-action="DataGridToolAction.CancelEdit" type="button" class="btn btn-light btn-flat mr-auto">
                <span>@T("Common.Cancel")</span>
            </button>
        </toolbar-group>
        <zone name="datagrid_toolbar_alpha"></zone>
        <toolbar-group class="omega"></toolbar-group>
        <zone name="datagrid_toolbar_omega"></zone>
        <toolbar-group>
            <button datagrid-action="DataGridToolAction.DeleteSelectedRows" type="button" class="btn btn-danger no-anims btn-flat">
                <i class="far fa-trash-alt"></i>
                <span>@T("Admin.Common.Delete.Selected")</span>
            </button>
        </toolbar-group>
    </toolbar>
    <columns>
        @*INFO: I don't want to hide any columns here so the shop admin doesn't miss any oportunities to configure his rates.*@
        <column for="StoreName" readonly entity-member="StoreId" />
        <column for="CountryName" readonly entity-member="CountryId" />
        <column for="StateProvinceName" readonly entity-member="StateProvinceId" />
        <column for="Zip" />
        <column for="ShippingMethodName" entity-member="ShippingMethodId" width="1fr" readonly hideable="false" />
        <column for="From" />
        <column for="To" />
        <column for="UsePercentage" halign="center" />
        <column for="ShippingChargePercentage" />
        <column for="ShippingChargeAmount" />
        <column for="BaseCharge" />
        <column for="MaxCharge" />
    </columns>
    <row-commands>
        <a datarow-action="DataRowAction.InlineEdit">@T("Common.Edit")</a>
        <a datarow-action="DataRowAction.Delete">@T("Common.Delete")</a>
    </row-commands>
</datagrid>

<modal id="addrecord-window" sm-render-at-page-end="true" sm-size="Large">
    <modal-header sm-title="@T("Plugins.Shipping.ByTotal.AddNewRecordTitle")"></modal-header>
    <modal-body>
        <form action="#">
            <div class="adminContent">
                <div class="adminRow">
					<div class="adminTitle">
                        <smart-label asp-for="StoreId" />
					</div>
                    <div class="adminData">
                        <select asp-for="StoreId" asp-items="ViewBag.AvailableStores" asp-placeholder="*"></select>
                        <span asp-validation-for="StoreId"></span>
                    </div>
				</div>
                <div class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="CountryId" />
                    </div>
                    <div class="adminData">
                        <editor asp-for="CountryId"
                                class="country-input country-selector"
                                asp-template="Countries"
                                asp-additional-viewdata='new { placeholder = "*" }'
                                data-region-control-selector="#@Html.IdFor(x => x.StateProvinceId)"
                                data-states-ajax-url="@Url.Action("StatesByCountryId", "Common", new { area = string.Empty })"
                                data-addEmptyStateIfRequired="true" />
                        <span asp-validation-for="CountryId"></span>
                    </div>
                </div>
                <div class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="StateProvinceId" />
                    </div>
                    <div class="adminData">
                        <select asp-for="StateProvinceId" asp-items="ViewBag.AvailableStates" asp-placeholder="*"></select>
                        <span asp-validation-for="StateProvinceId"></span>
                    </div>
                </div>
                <div class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="Zip" />
                    </div>
                    <div class="adminData">
                        <input asp-for="Zip" />
                        <span asp-validation-for="Zip"></span>
                    </div>
                </div>
                <div class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="ShippingMethodId" />
                    </div>
                    <div class="adminData">
                        <select asp-for="ShippingMethodId" asp-items="ViewBag.AvailableShippingMethods"></select>
                        <span asp-validation-for="ShippingMethodId"></span>
                    </div>
                </div>
                <div class="adminRow">
					<div class="adminTitle">
                        <smart-label asp-for="From" />
					</div>
					<div class="adminData">
                        <editor asp-for="From" sm-postfix="@ViewBag.PrimaryStoreCurrencyCode" />
                        <span asp-validation-for="From"></span>
					</div>
				</div>
				<div class="adminRow">
					<div class="adminTitle">
                        <smart-label asp-for="To" />
					</div>
					<div class="adminData">
                        <editor asp-for="To" sm-postfix="@ViewBag.PrimaryStoreCurrencyCode" />
                        <span asp-validation-for="To"></span>
					</div>
				</div>
				<div class="adminRow">
					<div class="adminTitle">
                        <smart-label asp-for="UsePercentage" />
					</div>
					<div class="adminData">
                        <input asp-for="UsePercentage" />
                        <span asp-validation-for="UsePercentage"></span>
					</div>
				</div>
				<div class="adminRow" id="pnlAddShippingChargePercentage">
					<div class="adminTitle">
                        <smart-label asp-for="ShippingChargePercentage" />
					</div>
					<div class="adminData">
                        <editor asp-for="ShippingChargePercentage" />
                        <span asp-validation-for="ShippingChargePercentage"></span>
					</div>
				</div>
				<div class="adminRow" id="pnlAddBaseCharge">
					<div class="adminTitle">
                        <smart-label asp-for="BaseCharge" />
					</div>
					<div class="adminData">
                        <editor asp-for="BaseCharge" sm-postfix="@ViewBag.PrimaryStoreCurrencyCode" />
                        <span asp-validation-for="BaseCharge"></span>
					</div>
				</div>
				<div class="adminRow" id="pnlAddMaxCharge">
					<div class="adminTitle">
                        <smart-label asp-for="MaxCharge" />
					</div>
					<div class="adminData">
                        <editor asp-for="MaxCharge" sm-postfix="@ViewBag.PrimaryStoreCurrencyCode" />
                        <span asp-validation-for="MaxCharge"></span>
					</div>
				</div>
				<div class="adminRow" id="pnlAddShippingChargeAmount">
					<div class="adminTitle">
                        <smart-label asp-for="ShippingChargeAmount" />
					</div>
                    <div class="adminData">
                        <editor asp-for="ShippingChargeAmount" sm-postfix="@ViewBag.PrimaryStoreCurrencyCode" />
                        <span asp-validation-for="ShippingChargeAmount"></span>
                    </div>
				</div>
            </div>
        </form>
    </modal-body>
    <modal-footer>
        <button type="button" class="btn btn-secondary btn-flat" data-dismiss="modal">
            <span>@T("Admin.Common.Cancel")</span>
        </button>
        <button id="add-shipping-rate-by-total" type="submit" class="btn btn-primary">
            <i class="fa fa-check"></i>
            <span>@T("Common.AddNew")</span>
        </button>
    </modal-footer>
</modal>

<script sm-target-zone="scripts" data-origin="shipping-rate-by-total-grid">
    $(function () {
        $("#@Html.IdFor(model => model.UsePercentage)").click(toggleAddUsePercentage);
        toggleAddUsePercentage();

        function toggleAddUsePercentage() {
            if ($('#@Html.IdFor(model => model.UsePercentage)').is(':checked')) {
                $('#pnlAddShippingChargePercentage').show();
                $('#pnlAddBaseCharge').show();
                $('#pnlAddMaxCharge').show();
                $('#pnlAddShippingChargeAmount').hide();
            }
            else {
                $('#pnlAddShippingChargePercentage').hide();
                $('#pnlAddBaseCharge').hide();
                $('#pnlAddMaxCharge').hide();
                $('#pnlAddShippingChargeAmount').show();
            }
		}

        $('#add-shipping-rate-by-total').on("click", function (e) {
			e.preventDefault();

			var form = $("#addrecord-window").find("form");

			$.ajax({
				cache: false,
				type: 'POST',
				url: '@Url.Action("AddShippingRateByTotal")',
				data: form.serialize(),
				dataType: 'json',
				success: function (data) {
                    window["shipping-rates-bytotal-grid"].$children[0].read();
				},
				error: function (xhr, ajaxOptions, thrownError) {
					alert('Failed to add record.');
				},
				complete: function () {
					$('#addrecord-window').modal('hide');
				}
			});
			return false;
		});
    });
</script>